﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ImageRecognition2.Action;
using LowLevelGraphics.CustomControls;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Filter
    /// </summary>
    public partial class FormFilter : Form, IFormFilter
    {
        /// <summary>
        /// Holds the original image
        /// </summary>
        protected Image m_ImageOriginal = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormFilter"/> class.
        /// </summary>
        public FormFilter()
        {
            InitializeComponent();
            InitializeEventHandlers();
            InitializeControls();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FormFilter"/> class.
        /// </summary>
        public FormFilter(Image image, int _nMin, int _nMax)
        {
            InitializeComponent();
            InitializeEventHandlers();
            InitializeControls();

            imgOriginal = image;
            pictureBox1.BackgroundImage = imgOriginal;
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            trackbAmount.Minimum = _nMin;
            trackbAmount.Maximum = _nMax;
            tbAmount.Text = _nMin.ToString();
            // set Slider Attributes
            trackbZoomSlider.Minimum = 1;
            trackbZoomSlider.Maximum = 10;
            trackbZoomSlider.SmallChange = 1;
            trackbZoomSlider.LargeChange = 1;

            this.trackbZoomSlider.Scroll += new EventHandler(trackbZoomSlider_Scroll);
            this.trackbAmount.Scroll += new EventHandler(trackbAmount_Scroll);
        }

        /// <summary>
        /// For IFormFilter to work
        /// </summary>
        public SelectablePictureBox PictureBox
        {
            get { return this.pictureBox1; }
        }

        private void InitializeControls()
        {
            // set Picture Box Attributes
            pictureBox1.BackgroundImageLayout = ImageLayout.None;
            pictureBox1.Zoom = 1.0f;
        }

        private void InitializeEventHandlers()
        {
            GuiHelper.SetEventHandler(this.noneToolStripMenuItem1, new ActionInterpolationMode(InterpolationMode.NearestNeighbor));
            GuiHelper.SetEventHandler(this.lowToolStripMenuItem, new ActionInterpolationMode(InterpolationMode.Low));
            GuiHelper.SetEventHandler(this.highToolStripMenuItem, new ActionInterpolationMode(InterpolationMode.High));
        }

        /// <summary>
        /// Gets or sets the img original.
        /// </summary>
        /// <value>The img original.</value>
        public Image imgOriginal
        {
            get { return m_ImageOriginal; }
            set { m_ImageOriginal = value; }
        }

        /// <summary>
        /// Handles the Scroll event of the trackbAmount control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void trackbAmount_Scroll(object sender, EventArgs e)
        {
            UpdateScroll();
        }

        private void UpdateScroll()
        {
            this.tbAmount.Text = trackbAmount.Value.ToString();
            pictureBox1.Invalidate();
        }

        /// <summary>
        /// Handles the Scroll event of the trackbZoomSlider control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void trackbZoomSlider_Scroll(object sender, EventArgs e)
        {
            if (imgOriginal == null) return;
            if (trackbZoomSlider.Value > 0)
            {
                pictureBox1.Zoom = trackbZoomSlider.Value / 100.0f;
            }
        }

        /// <summary>
        /// Handles the Click event of the cbOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the cbCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the cb100Percent control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cb100Percent_Click(object sender, EventArgs e)
        {
            this.BackgroundImageLayout = ImageLayout.Zoom;
        }

        /// <summary>
        /// Handles the KeyDown event of the tbAmount control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void tbAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
            {
                trackbAmount.Value = Convert.ToInt32(tbAmount.Text);
                UpdateScroll();
            }
        }

        /// <summary>
        /// Handles the Click event of the zoomToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
        }

        /// <summary>
        /// Handles the Click event of the centerToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void centerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Center;
        }

        /// <summary>
        /// Handles the Click event of the tileToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void tileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Tile;
        }

        /// <summary>
        /// Handles the Click event of the stretchToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void stretchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
        }

        /// <summary>
        /// Handles the Click event of the noneToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void noneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.pictureBox1.BackgroundImageLayout = ImageLayout.None;
        }
    }
}
